const { login }=require('../controller/user')

const setCookieExpireTime=function(){
    const date=new Date();
    date.setTime(date.getTime()+10*1000)
    return date.toGMTString()
}

const handleUserRouter=function(req,res){
    if(req.method==='GET'&&req.path==='/api/user/login'){
        const {username,password}=req.query;
        const loginResult=login(username,password)
        if(loginResult){
            res.setHeader('Set-Cookie',`username=${username};path=/;expires=${setCookieExpireTime()}`)
            return{
                errorCode:0,
                data:req.query
            }
        }
        return{
            errorCode:-1,
            msg:'用户名或密码错误'
        }
     
    }
}

module.exports=handleUserRouter